﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using VietTrips.Domain.Contexts;
using VietTrips.Domain.Interfaces;
using VietTrips.Domain.Models;

namespace VietTrips.Domain.Repositories
{
    public class TourAgencyRepository : ITourAgencyRepository
    {
        readonly EntitiesContexts _contexts = new EntitiesContexts();

        public void Dispose()
        {
            if (_contexts != null)
            {
                _contexts.Dispose();
            }
        }

        public IQueryable<TourAgency> AllActive
        {
            get { return _contexts.TourAgencies.Where(t => !t.IsDeleted); }
        }

        public IQueryable<TourAgency> All
        {
            get { return _contexts.TourAgencies; }
        }

        public IQueryable<TourAgency> AllIncluding(params Expression<Func<TourAgency, object>>[] includeExpressions)
        {
            // TODO: Làm sau
            return null;
        }

        public TourAgency Find(int id)
        {
            return _contexts.TourAgencies.Find(id);
        }

        public void Insert(TourAgency entity)
        {
            _contexts.TourAgencies.Add(entity);
        }

        public void Update(TourAgency entity)
        {
            _contexts.Entry(entity).State = EntityState.Modified;
            this.Save();
        }

        public void Delete(int id)
        {
            var tourAgency = _contexts.TourAgencies.Find(id);
            if (tourAgency == null) return;
            tourAgency.IsDeleted = true;
            tourAgency.LastModified = DateTime.Now;
            this.Update(tourAgency);
        }

        public void Save()
        {
            _contexts.SaveChanges();
        }
    }

    public interface ITourAgencyRepository : IBaseRepository<TourAgency>
    {
        IQueryable<TourAgency> AllActive { get; }
    }
}
